package com.example.chatapp.controller;

import com.example.chatapp.common.ApiResponse;
import com.example.chatapp.common.BusinessException;
import com.example.chatapp.model.User;
import com.example.chatapp.repository.UserRepository;
import com.example.chatapp.service.FriendshipService;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/friends")
public class FriendshipController {
    
    private final FriendshipService friendshipService;
    private final UserRepository userRepository;
    
    public FriendshipController(FriendshipService friendshipService, UserRepository userRepository) {
        this.friendshipService = friendshipService;
        this.userRepository = userRepository;
    }
    
    /**
     * 添加好友
     */
    @PostMapping("/add")
    public ApiResponse<String> addFriend(@RequestParam String friendUsername,
                                        HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username == null) {
            throw new BusinessException(401, "未登录");
        }
        
        User user = userRepository.findByUsername(username)
                .orElseThrow(() -> new BusinessException(404, "用户不存在"));
        
        friendshipService.addFriend(user.getId(), friendUsername);
        
        return ApiResponse.success("添加好友成功");
    }
    
    /**
     * 删除好友
     */
    @PostMapping("/remove")
    public ApiResponse<String> removeFriend(@RequestParam String friendUsername,
                                           HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username == null) {
            throw new BusinessException(401, "未登录");
        }
        
        User user = userRepository.findByUsername(username)
                .orElseThrow(() -> new BusinessException(404, "用户不存在"));
        
        friendshipService.removeFriend(user.getId(), friendUsername);
        
        return ApiResponse.success("删除好友成功");
    }
    
    /**
     * 获取好友列表
     */
    @GetMapping("/list")
    public ApiResponse<List<Map<String, Object>>> getFriendList(HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username == null) {
            throw new BusinessException(401, "未登录");
        }
        
        User user = userRepository.findByUsername(username)
                .orElseThrow(() -> new BusinessException(404, "用户不存在"));
        
        List<User> friends = friendshipService.getFriendList(user.getId());
        
        // 转换为前端需要的格式
        List<Map<String, Object>> friendList = friends.stream()
                .map(friend -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("username", friend.getUsername());
                    map.put("id", friend.getId());
                    map.put("createdAt", friend.getCreatedAt());
                    return map;
                })
                .toList();
        
        return ApiResponse.success(friendList);
    }
    
    /**
     * 检查是否是好友
     */
    @GetMapping("/check")
    public ApiResponse<Boolean> checkFriend(@RequestParam String friendUsername,
                                           HttpSession session) {
        String username = (String) session.getAttribute("username");
        if (username == null) {
            throw new BusinessException(401, "未登录");
        }
        
        User user = userRepository.findByUsername(username)
                .orElseThrow(() -> new BusinessException(404, "用户不存在"));
        
        boolean isFriend = friendshipService.isFriend(user.getId(), friendUsername);
        
        return ApiResponse.success(isFriend);
    }
}
